package com.example.excel.poi.util;

import android.text.TextUtils;

import com.example.excel.base.CommonCallback;
import com.example.excel.bean.InventoryBean;
import com.example.excel.db.InventoryController;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.util.Arrays;
import java.util.List;

class PoiUtil {

    public static int importExcel(String filepath, CommonCallback<List<String>> commonCallback) {
        try {
            // 通过Workbook.getWorkbook(inputStream);获取到整个excel
            XSSFWorkbook workbook = new XSSFWorkbook(new File(filepath));
            //获取第一张excel数据表。
            XSSFSheet sheet = workbook.getSheetAt(0);
            //默认第一行为标题行，index = 0
            //获取该表中有多少行数据。
            int rowsCount = sheet.getPhysicalNumberOfRows();
            int cellsCount = sheet.getRow(0).getPhysicalNumberOfCells();
            int total = 0;
            String[] titles = new String[cellsCount];
            for (int i = 0; i < rowsCount; i++) {
                Row row = sheet.getRow(i);
                String faNo0 = getCellAsString(row,0);
                String faNo1 = getCellAsString(row,1);
                String faNo2 = getCellAsString(row,2);
                String faNo3 = getCellAsString(row,3);
                String faNo4 = getCellAsString(row,4);
                String faNo5 = getCellAsString(row,5);
                String faNo6 = getCellAsString(row,6);
                String faNo7 = getCellAsString(row,7);
                String faNo8 = getCellAsString(row,8);
                String faNo9 = getCellAsString(row,9);
                String faNo10 =getCellAsString(row,10);
                String faNo11 =getCellAsString(row,11);
                String faNo12 =getCellAsString(row,12);
                String faNo13 =getCellAsString(row,13);
                String faNo14 =getCellAsString(row,14);
                String faNo15 =getCellAsString(row,15);
                String faNo16 =getCellAsString(row,16);
                String faNo17 =getCellAsString(row,17);
                if (i == 0) {
                    titles[0] = faNo0;
                    titles[1] = faNo1;
                    titles[2] = faNo2;
                    titles[3] = faNo3;
                    titles[4] = faNo4;
                    titles[5] = faNo5;
                    titles[6] = faNo6;
                    titles[7] = faNo7;
                    titles[8] = faNo8;
                    titles[9] = faNo9;
                    titles[10] = faNo10;
                    titles[11] = faNo11;
                    titles[12] = faNo12;
                    titles[13] = faNo13;
                    titles[14] = faNo14;
                    titles[15] = faNo15;
                    titles[16] = faNo16;
                    titles[17] = faNo17;
                    commonCallback.onResult(Arrays.asList(titles));
                    continue;
                }
                // 盘点单号	资产编号	资产名称	品牌	规格型号	资产类别	计量单位	使用部门	放置地点	使用人	资产状态	购置日期	质保期	启用日期	资产价值	供应商	盘点状态	备注
                InventoryBean entity = new InventoryBean();
                entity.setFaNo(faNo0);
                entity.setFaCode(faNo1);
                entity.setFaName(faNo2);
                entity.setBrandName(faNo3);
                entity.setFaModel(faNo4);
                entity.setFaType(faNo5);
                entity.setFaUnit(faNo6);
                entity.setUseDept(faNo7);
                entity.setUsePlace(faNo8);
                entity.setUseUser(faNo9);
                entity.setProjectStatus(faNo10);
                entity.setCreateData(faNo11);
                entity.setPeriodData(faNo12);
                entity.setEnableDate(faNo13);
                entity.setFaPrice(faNo14);
                entity.setSupplier(faNo15);
                entity.setFaFlag(TextUtils.isEmpty(faNo16) || TextUtils.equals(faNo16, "未盘点") ? 0 : 1);
                entity.setRemark(faNo17);
                boolean b = InventoryController.getInstance().insert(entity);
                if (!b) {
                    return -1;
                }
                total += 1;
            }
            return total;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    protected static String getCellAsString(Row row, int c) {
        Cell cell = row.getCell(c);
        if(cell != null){
            cell.setCellType(Cell.CELL_TYPE_STRING);
            return  "" + cell.getStringCellValue();
        }
        return  "";
    }
}
